package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.MobileOrderService;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.handler.MessageContext;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 手机 订单 预约controller
 * 任务11新增
 */
@RestController
@Slf4j
@RequestMapping("/mobile/order")
public class MobileOrderController {

    @Autowired
    private MobileOrderService mobileOrderService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetMealService setMealService;

    @GetMapping("findById")
    public Result findById(Integer id){
        //查询orderType 预约方式  &   orderDate 预约日期  任务11新增
        Order order = this.mobileOrderService.findById(id);
        //查询member 预约会员名    任务11新增

        Member member = this.memberService.findById(order.getMemberId());
        //查询setmeal 预约套餐名称  任务11新增
        Setmeal setmeal = this.setMealService.findById(order.getSetmealId());

        //封装 返回result
        Map<String, Object> map = new HashMap<>();
        map.put("setmeal",setmeal.getName());
                            //按照 格式 封装日期
        map.put("ordrDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
        if (member.getName()!=null && member.getName().length()>0){
            map.put("member",member.getName());
        }else{
            map.put("member",member.getPhoneNumber());
        }
        map.put("orderType",order.getOrderType());
        return  new Result(true, MessageConst.QUERY_ORDER_SUCCESS , map);
    }
}
